package service

import (
	"fmt"
	"mime/multipart"
	"strconv"
	"sync"
	"time"
	"user_video/model"
	"user_video/sql"
	"user_video/util"

	"log"
)

var (
	VideoProcess = sync.Map{}
)

type VideoService struct {
	UserId    uint
	VideoURL  string
	StartTime string
	EndTime   string
}

func generateId(in uint) (id string) {
	in_id := strconv.Itoa(int(in))
	timestamp := time.Now().Unix() % 100000000 // 获取当前时间戳（以秒为单位）
	id = fmt.Sprintf("%s%08d", in_id, timestamp)
	return id
}
func (v *VideoService) UploadVideo(userId uint, file *multipart.FileHeader) error {
	videoId := generateId(userId)
	if err := util.Write_local(file, videoId); err != nil {
		return err
	}
	return nil
}
func (v *VideoService) Clip_Video(userId uint, video_url, startTime, endTime string) (video_id string, err error) {
	t := time.Now()
	video_id = generateId(userId)
	video := model.Video{
		userId,
		video_id,
		&t,
		nil,
		"",
	}
	sql_instance := sql.GetInstance()
	if err := sql_instance.Create(&video).Error; err != nil {
		log.Println("error Failed to save clip request")
		return "", err
	}
	//后端处理fmmpeg

	return video_id, nil
}
func (v *VideoService) GetVideoResult(video_id string) (url string, err error) {
	var video model.Video
	sql_instance := sql.GetInstance()
	sql_instance.Where("VideoID = ?", video_id).Find(video)

	return video.Url, err
}
